<?php

    namespace app\listen\controller;

    use app\listen\model\UserLove;

    class Comment extends \app\common\controller\Base {
        private $commentModel;
        /**
         * @return mixed
         */
        public function getCommentModel(): \app\listen\model\Comment {
            if (empty($this->commentModel)) {
                $this->commentModel = model('listen/Comment');
            }
            return $this->commentModel;
        }

        public function filter($info, $uid, $option) {
            if (!empty($info) && !empty($info['list'])) {
                $info['list'] = array_map(function ($value) use ($uid, $option) {
                    if (!empty($value['id'])) {
                        $value['loveCount'] = $this->getCommentModel()->getLoveCount($value['id']);
                    }
                    if (!empty($uid)) {
                        $value['myLove'] = $this->getCommentModel()->getUseLove($uid, $value['id']);
                    }
                    if (isset($value['loveCount']) && !empty($option['lovers'])) {
                        $value['lovers'] = $this->getCommentModel()->getLovers($value['id'], 1, 4);
                    }
                    return $value;
                }, $info['list']);
            }
            return $info;
        }

        /**
         * 评论列表
         *
         */
        public function commentList() {
            $data = $this->getParams();
            $page = $data['page'] ?? 1;
            $rows = $data['rows'] ?? 10;
            $voiceId = $data['voiceId'] ?? null;// 语音id
            $uid = $data['uid'] ?? null;
            $toCommentId = $data['toCommentId'] ?? null;// 针对的评论id
            $info = $this->getCommentModel()->commentList($voiceId, $toCommentId, $page, $rows);
            $info = $this->filter($info, $uid, $data);
            return api_return(10011, $info);
        }

        /**
         * 评论列表
         *
         */
        public function addComment() {
            $data = $this->getParams();
            $uid = $data['uid'] ?? null;
            $voiceId = $data['voiceId'] ?? null;
            $toCommentId = $data['toCommentId'] ?? null;// 针对的评论id
            $content = $data['content'] ?? null;
            $result = $this->getCommentModel()->addComment($uid, $voiceId, $content, $toCommentId);
            $recipientUid = $data['recipientUid'] ?? null;
            // 通知消息
            if ($result > 0 && isset($recipientUid)) {

                // 回复
                if (isset($toCommentId)) {
                    self::https_request($_SERVER['HTTP_HOST'] . '/api/v1.0/message/add', ['type' => Message::$MESSAGE_TYPE_COMMENT, 'sendUid' => $uid, 'recipientUid' => $recipientUid, 'chainId' => $toCommentId]);
                } else {// 评论
                    self::https_request($_SERVER['HTTP_HOST'] . '/api/v1.0/message/add', ['type' => Message::$MESSAGE_TYPE_COMMENT, 'sendUid' => $uid, 'recipientUid' => $recipientUid, 'chainId' => $voiceId]);
                }

            }
            if (false !== $result) {
                return api_return(10011, $result);
            }
            return api_return(-2017, $result);
        }

        /**
         * 删除
         *
         */
        public function delComment() {
            $data = $this->getParams();
            $id = $data['id'] ?? null;
            if (empty($id)) {
                return api_return(-1, 'id');
            }
            $result = $this->getCommentModel()->delComment($id);
            if (false !== $result) {
                return api_return(10011, $result);
            }
            return api_return(-2017, $result);
        }
        // 喜欢评论
        public function loveComment() {
            $data = $this->getParams();
            $commentId = $data['commentId'] ?? null;
            $uid = $data['uid'] ?? null;
            if (empty($commentId) || empty($uid)) {
                return api_return(-1, 'commentId or uid');
            }
            $result = $this->getCommentModel()->loveComment($uid, $commentId);
            if (false !== $result) {
                return api_return(10011, $result);
            }
            return api_return(-2017, $result);
        }
        // 不喜欢评论
        public function disloveComment() {
            $data = $this->getParams();
            $commentId = $data['commentId'] ?? null;
            $uid = $data['uid'] ?? null;
            if (empty($commentId) || empty($uid)) {
                return api_return(-1, 'commentId or uid');
            }
            $result = $this->getCommentModel()->disloveComment($uid, $commentId);
            if (false !== $result) {
                return api_return(10011, $result);
            }
            return api_return(-2017, $result);
        }
    }